//
//  LFShopListViewController.m
//  LoveFruit
//
//  Created by 马海江 on 16/9/27.
//  Copyright © 2016年 张令林. All rights reserved.
//

#import "LFShopListViewController.h"
#import "LFShopListTableViewCell.h"
#import "LFShopCollectModel.h"
static NSString *cellid = @"abcd";

@interface LFShopListViewController ()

@property (nonatomic, weak)LFShopListTableViewCell *cell1;
@property (nonatomic, weak)LFShopListTableViewCell *cell2;
@property (nonatomic, weak)LFShopListTableViewCell *cell3;

@property (nonatomic, weak)UITableView *tableview;
@end

@interface LFShopListViewController ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation LFShopListViewController
//- (void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
//    self.navigationController.navigationBar.hidden = NO;
//}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    
    if (self.cell1.isCollect == YES) {
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationDIZhi1" object:nil userInfo:nil];
        
        LFShopCollectModel *model = [LFShopCollectModel new];
        model.name = self.cell1.name.text;
        model.time = self.cell1.time.text;
        model.address = self.cell1.address.text;
        
        NSString *tempPath = NSTemporaryDirectory();
        NSString *filePath = [tempPath stringByAppendingPathComponent:@"DiZhi1.data"];
        [NSKeyedArchiver archiveRootObject:model toFile:filePath];
    }
    if (self.cell2.isCollect == YES){
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationDIZhi2" object:nil userInfo:nil];
        
        LFShopCollectModel *model = [LFShopCollectModel new];
        model.name = self.cell2.name.text;
        model.time = self.cell2.time.text;
        model.address = self.cell2.address.text;
        
        NSString *tempPath = NSTemporaryDirectory();
        NSString *filePath = [tempPath stringByAppendingPathComponent:@"DiZhi2.data"];
        [NSKeyedArchiver archiveRootObject:model toFile:filePath];
    }
    if (self.cell3.isCollect == YES){
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationDIZhi3" object:nil userInfo:nil];
        
        LFShopCollectModel *model = [LFShopCollectModel new];
        model.name = self.cell3.name.text;
        model.time = self.cell3.time.text;
        model.address = self.cell3.address.text;
        
        NSString *tempPath = NSTemporaryDirectory();
        NSString *filePath = [tempPath stringByAppendingPathComponent:@"DiZhi3.data"];
        [NSKeyedArchiver archiveRootObject:model toFile:filePath];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.cell1.isCollect = NO;
    self.cell2.isCollect = NO;
    self.cell3.isCollect = NO;
    
    self.navigationItem.title = @"新增店铺";
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barBuutonItemWithTarget:self action:@selector(backItem) icon:@"btn_backItem" highlighticon:nil];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView registerNib:[UINib nibWithNibName:@"LFShopListTableViewCell" bundle:nil] forCellReuseIdentifier:cellid];
    // 取消系统分割线
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.tableview = tableView;
    [self.view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.offset(35);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        LFShopListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        cell.name.text = @"爱鲜蜂龙乡店（吉利发超市）";
        cell.time.text = @"营业时间：9：00--22：00";
        cell.address.text = @"昌平区龙乡东区10号楼对面";
        cell.distance.text = @"0.31km";
        cell.collect.tag = 1;
//        cell.isCollect = NO;
        self.cell1 = cell;
//        cell.collect.backgroundColor = [UIColor colorWithHex:0xffd600];
        return cell;
    }else if (indexPath.row == 1){
        LFShopListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        cell.name.text = @"爱鲜蜂上奥世纪店（世纪华联）";
        cell.time.text = @"营业时间：9：00--23：00";
        cell.address.text = @"昌平区回龙观镇建材城西路87号院3号楼1层101号";
        cell.distance.text = @"0.49km";
        cell.collect.tag = 2;
//        cell.isCollect = NO;
        self.cell2 = cell;
//        cell.collect.backgroundColor = [UIColor colorWithHex:0xffd600];
        return cell;
    }else{
        LFShopListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        cell.name.text = @"爱鲜蜂水利宿舍店（雯浩晟源网络超市）";
        cell.time.text = @"营业时间：9：00--22：00";
        cell.address.text = @"北京市海淀区安宁庄后街16号水利宿舍底商";
        cell.distance.text = @"1.07km";
        cell.collect.tag = 3;
//        cell.isCollect = NO;
        self.cell3 = cell;
//        cell.collect.backgroundColor = [UIColor colorWithHex:0xffd600];
        return cell;
    }
}
- (IBAction)collectButtonClick:(UIButton *)sender {
    [SVProgressHUD show];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (sender.tag == 1) {
            [self buttonValueChange:self.cell1];
        }else if (sender.tag == 2){
            [self buttonValueChange:self.cell2];
        }else{
            [self buttonValueChange:self.cell3];
        }
        [SVProgressHUD dismiss];
        [self.tableview reloadData];
    });
}
- (void) buttonValueChange:(LFShopListTableViewCell *)cell{
    if (cell.isCollect == YES) {
        cell.collect.backgroundColor = [UIColor colorWithHex:0xffd600];
        [cell.collect setTitle:@"+收藏" forState:UIControlStateNormal];
        [cell.collect setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        cell.isCollect = NO;
    }else{
        cell.collect.backgroundColor = [UIColor lightGrayColor];
        [cell.collect setTitle:@"已收藏" forState:UIControlStateNormal];
        [cell.collect setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        cell.isCollect = YES;
        
        
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"附近的店铺";
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}
- (void)backItem{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
